<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select v-model="listQuery.room_id" clearable placeholder="宿舍号" class="filter-item">
        <el-option v-for="(value, index) in room" :key="index" :value="index" :label="value"/>
      </el-select>
      <el-input v-model="listQuery.username" clearable placeholder="申请人" style="width: 150px" class="filter-item"/>
      <el-select v-model="listQuery.status" clearable class="filter-item">
        <el-option label="未维修" value="0"/>
        <el-option label="已维修" value="1"/>
      </el-select>
      <el-button type="primary" class="filter-item" @click="handleFilter">搜索</el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading" border highlight-current-row>
      <el-table-column label="ID">
        <template slot-scope="scope">{{ scope.row.id }}</template>
      </el-table-column>
      <el-table-column label="宿舍">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <p>宿舍成员: </p>
            <p v-for="(value) in scope.row.people" :key="value">{{ value }}</p>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.room }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="申请人">
        <template slot-scope="scope">{{ scope.row.username }}</template>
      </el-table-column>
      <el-table-column label="图片">
        <template slot-scope="scope">
          <img :src="scope.row.image_url" style="max-width: 100%;max-height: 100%;">
        </template>
      </el-table-column>
      <el-table-column label="内容">
        <template slot-scope="scope">{{ scope.row.content }}</template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope"> {{ scope.row.status == 0 ? '未维修' : '已维修' }}</template>
      </el-table-column>
      <el-table-column label="提交时间">
        <template slot-scope="scope">{{ scope.row.create_time }}</template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button :disabled="scope.row.status == 1" type="primary" size="mini" @click="handleReview(scope.row)">
            {{ scope.row.status == 0 ? '未维修' : '已维修' }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="fetchData"/>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'
import { getList, save } from '@/api/baoxiu'
export default {
  name: 'Index',
  components: { Pagination },
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 20,
        room_id: '',
        username: '',
        status: ''
      },
      list: [],
      listLoading: true,
      dialogVisible: false,
      total: 0,
      room: []
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      getList(this.listQuery).then(res => {
        this.listLoading = false
        this.list = res.data.list
        this.total = res.data.total
        this.room = res.data.room
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.fetchData()
    },
    handleReview(row) {
      this.$confirm('是否将维修状态改为已维修？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        save({ id: row.id, room_id: row.room_id }).then(response => {
          this.$notify({
            title: '成功',
            message: '处理成功',
            type: 'success',
            duration: 2000
          })
          this.fetchData()
        })
      }).catch(() => {
        this.$message({
          message: '已取消',
          type: 'info'
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
